# 应用配置信息
import numpy as np
import torch
from matplotlib.font_manager import FontProperties
from apps.fmcw.conf.app_const import AppConst as AC

class AppConfig(object):
    def __init__(self):
        self.name = 'apps.fmcw.conf.app_config.AppConfig'

    font_path = './libs/SimHei.ttf'  # SimHei字体的路径
    font_prop = FontProperties(fname=font_path)
    # plt.title(f'中文内容', fontproperties=AF.font_prop)

    t_sys = 0
    BW = 150e6 #bandwidth
    fc = 77e9 #carrier frequency
    numADC = 256 # of adc samples
    numChirps = 256 # of chirps per frame
    numCPI = 10
    T = 10e-6 # PRI 
    PRF = 1/T
    F = numADC/T # sampling frequency
    dt = 1/F #sampling interval
    slope = BW/T
    lambda_ = AC.c/fc # 波长
    N = numChirps*numADC*numCPI # total # of adc samples
    N_CPI = numChirps*numADC # total # of adc samples
    times = None # np.linspace(0, T * numChirps * numCPI, N) # linspace(0,T*numChirps*numCPI,N) # time axis, one frame
    t_onePulse = None # np.arange(0, dt * numADC, dt)
    numTX = 1
    numRX = 8
    Vmax = lambda_/(T*4) # Max Unamb velocity m/s
    DFmax = 1/2*PRF # = Vmax/(c/fc/2); % Max Unamb Dopp Freq
    dR = AC.c/(2*BW) # range resol
    Rmax = F*AC.c/(2*slope) # TI's MIMO Radar doc
    Rmax2 = AC.c/2/PRF #lecture 2.3
    dV = lambda_/(2*numChirps*T) # velocity resol, lambda/(2*framePeriod)
    d_rx = lambda_/2 #dist. between rxs
    d_tx = 4*d_rx # dist. between txs

    N_Dopp = numChirps # length of doppler FFT
    N_range = numADC #length of range FFT
    N_azimuth = numTX*numRX
    R = np.arange(0, Rmax, dR)  # range axis
    V = np.linspace(-Vmax, Vmax, numChirps)  # Velocity axis
    ang_ax = np.arange(-90, 91)  # angle axis
    FRAME_LEN = numADC*numChirps*numTX*numRX
    tx_loc = np.zeros((numTX, 3))
    rx_loc = np.zeros((numRX, 3))

    delays_tar1 = np.zeros((numTX, numRX, N_CPI))
    delays_tar2 = np.zeros((numTX, numRX, N_CPI))
    mixed = np.zeros((numTX, numRX, N_CPI), dtype=complex)
    signal_t = np.zeros(N_CPI, dtype=complex)
    signal_1 = np.zeros(N_CPI, dtype=complex)
    signal_2 = np.zeros(N_CPI, dtype=complex)

    RDMs = np.zeros((N_range, N_Dopp, numTX * numRX), dtype=complex)

    numGuard = 2 # of guard cells
    numTrain = numGuard*2 # of training cells
    P_fa = 1e-5 # desired false alarm rate
    SNR_OFFSET = -5 # dB

    d = 0.5

    # 目标相关参数
    tar1_loc = np.zeros((N_CPI, 3)) # FRAME_LEN为一帧的长度：numADC*numChirps*numTX*numRX
    tar2_loc = np.zeros((N_CPI, 3))

    r1_radial = 50
    tar1_angle = -15
    r1_y = np.cos(np.radians(tar1_angle)) * r1_radial
    r1_x = np.sin(np.radians(tar1_angle)) * r1_radial
    v1_radial = 10  # velocity 1
    v1_y = np.cos(np.radians(tar1_angle)) * v1_radial
    v1_x = np.sin(np.radians(tar1_angle)) * v1_radial
    r1 = [r1_x, r1_y, 0]

    r2_radial = 100
    tar2_angle = 10
    r2_y = np.cos(np.radians(tar2_angle)) * r2_radial
    r2_x = np.sin(np.radians(tar2_angle)) * r2_radial
    v2_radial = -15  # velocity 2
    v2_y = np.cos(np.radians(tar2_angle)) * v2_radial
    v2_x = np.sin(np.radians(tar2_angle)) * v2_radial
    r2 = [r2_x, r2_y, 0]
    ranges_ = [r1_radial, r2_radial]
    velocitys = [v1_radial, v2_radial]
    thetas = [tar1_angle, tar2_angle]

    # 
    MAX_OBJS = 3 # 最大识别目标数量
    OBJ_DIMS = 4 # 目标特征维度：概率、径向距离、径向速度、水平到达角
    R_MAX = Rmax
    R_MIN = 0.0
    V_MAX = Vmax
    V_MIN = -Vmax
    A_MAX = ang_ax[-1]
    A_MIN = ang_ax[0]
    Y_MIN = torch.tensor([0.0, R_MIN, V_MIN, A_MIN, 0.0, R_MIN, V_MIN, A_MIN, 0.0, R_MIN, V_MIN, A_MIN], dtype=torch.float32)
    Y_MAX = torch.tensor([1.0, R_MAX, V_MAX, A_MAX, 1.0, R_MAX, V_MAX, A_MAX, 1.0, R_MAX, V_MAX, A_MAX], dtype=torch.float32)

    #
    range_bin = torch.from_numpy(np.linspace(0, Rmax, numADC))
    velocity_bin = torch.from_numpy(np.linspace(-Vmax, Vmax, numChirps))
    theta_bin = torch.from_numpy(np.linspace(ang_ax[0], ang_ax[-1], numTX*numRX))
